-- cure_weak
-- created by panyl
-- 每若干回合给周围血量最低的怪物回复生命

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,

    -- 每回合触发
    trigger = function(who, prop, arg)
        local grid = DungeonM.getGridByPos(who:getOwner());
        local openRound = grid.openRound;
        if not openRound then
            return 0;
        end

        -- 当前回合
        round = CombatM.getRound();

        -- 没到起效回合
        if (round - openRound) % tonumber(prop[2]) ~= 0 then
            return 0;
        end

        local value = tonumber(prop[3]);

        local arounds = DungeonM.getAroundGrids(who:getOwner());
        local target;
        local targetHp = 100000000;
        for index = 1, #arounds do
            pos = arounds[index];
            
            local grid = DungeonM.getGridByPos(pos);
            local monster = grid.monster;
            local ok = true;
            
            -- 如果还未翻开，或者不是怪物，或者已经死亡
            if  not grid:isOpened() or
                not grid:isMonster() or
                not monster or
                monster:isDead() then
                ok = false;
            end
            
            if ok and targetHp > monster:getHp() then
                targetHp = monster:getHp();
                target = monster;
            end
        end
        
        if not target then
            return 0;
        end

        -- 治疗
        CombatM.receiveCure(who, target, value);
        
        return value;
    end,

    record = function(target, prop, path)
        return false;
    end 
};